﻿using BLL.CSocket;
using System;
using System.ServiceProcess;
using System.Threading;
using Tools;


namespace AgencyChargeService
{
    public partial class GasPaymentService : ServiceBase
    {
        public GasPaymentService()
        {
            InitializeComponent();
        }
        Thread thread = null;
        protected override void OnStart(string[] args)
        //public void OnStart()

        {
            LogHelper.WriteInfoLog("银行代缴费服务已启动");

            AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
            try
            {
                ClientThread.IsRun = true; 
                thread = new Thread(new ThreadStart(ClientThread.StartServer));
                thread.Start(); 
            }
            catch (Exception ex)
            {
                LogHelper.WriteErrorLog(ex.Message);
                thread = null;
                thread = new Thread(new ThreadStart(ClientThread.StartServer));
                thread.Start();
            }
        }

        void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
        {
            if (e.ExceptionObject != null)
            {
                Exception ex = e.ExceptionObject as Exception;
                LogHelper.WriteErrorLog(ex.Message);
            }
        }
        
        protected override void OnStop()
        {
            ClientThread.IsRun = false;
            LogHelper.WriteInfoLog("银行代缴费服务已关闭");            
        }
    }
}
